<?php

namespace App\Http\Controllers\Admin;


use App\Models\SaoPay;
use App\Models\Shop;

class SaoPayController extends BaseController
{


    public function __construct(SaoPay $saoPay, Shop $shop)
    {
        parent::__construct();
        $this->shop = $shop;
        $this->saoPay = $saoPay;
    }

    public function getIndex()
    {
        $keyword = trim(request('keyword', ''));
        $type = request('type', 0);
        $where = [];
        if ($keyword) {
            switch ($type) {
                case 1:
                    $where = ['id' => $keyword];
                    break;
                case 2:
                    $where = ['shop_name' => ['like', "%{$keyword}%"]];
                    break;
                case 3:
                    $where = ['boss_name' => ['like', "%{$keyword}%"]];
                    break;
                case 4:
                    $where = ['shop_phone' => ['like', "%{$keyword}%"]];
                    break;
                case 5:
                    $where = ['address' => ['like', "%{$keyword}%"]];
                    break;
                case 6:
                    $where = ['merchant_no' => ['like', "%{$keyword}%"]];
                    break;
            }
        }
        $shopFields = 'id,shop_name,boss_name,shop_phone,area_info,address';
        if ($type != 6) {
            $rows = $this->shop->_getJoinPageList('saoPay', $where, $shopFields, ['id' => 'asc'], 15, 10);
        } else {
            $rows = $this->saoPay->_getJoinPageList('shop', $where, '*', ['id' => 'asc'], 15, 10, $shopFields);
            if ($rows['data']) {
                $arr = [];
                foreach ($rows['data'] as $k=>$v) {
                    if(!$v['shop']) {
                        unset($rows['data'][$k]);
                        continue;
                    } else {
                        $tem = $v['shop'];
                        $tem['sao_pay'] = [
                            "id" => $v['id'],
                            "merchant_no" => $v['merchant_no'],
                        ];
                        $arr[] = $tem;
                    }
                }
                $rows['data'] = $arr;
            }
        }
        return view('admin.sao_pay.index', compact('rows', 'type', 'keyword'));
    }

    public function getEdit()
    {
        $shopId = request('shop_id', 0);
        $payId = request('pay_id', 0);
        $where['id'] = $shopId;
        $relationWhere = [];
        if ($payId) {
            $relationWhere['id'] = $payId;
        }
        $info = $this->shop->_getJionOne('saoPay', $where, 'id,shop_name', 'id,shop_id,merchant_no,terminal_id,token', $relationWhere);
        if ($info) {
            $info = $info->toArray();
        }
        return view('admin.sao_pay.edit', compact('info'));
    }

    public function postEdit()
    {
        $res = $this->saoPay->addOrUpdate();
        if (!$res) {
            return $this->error($this->saoPay->getError() ? $this->saoPay->getError() : '未知错误');
        }
        return $this->success('设置成功');
    }

}
